import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:mahjong/const/MahjongConst.dart';
import 'package:mahjong/const/MahjongSystemConst.dart';
import 'package:mahjong/entity/UserTouchBar.dart';
import 'package:mahjong/entity/card/MahjongCardEntity.dart';
import 'package:mahjong/notify/RightUserInfo.dart';
import 'package:mahjong/widget/LeftGameCommons.dart';
import 'package:mahjong/widget/MahjongCommon.dart';
import 'package:provider/provider.dart';
import 'SwapMahjongWidget.dart';

class RightGameWidget extends StatelessWidget {
  const RightGameWidget({
    Key key,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Consumer(builder:
        (BuildContext buildContext, RightUserInfo provider, Widget child) {
      if (provider.userId == null) {
        return Container();
      } else {
        return Stack(
          children: [
            // 头像的位置
            Positioned(
                top: ScreenUtil().screenHeight / 3,
                right: MahjongSystemConst.edgeMargin,
                child: SafeArea(
                  key: userLocation[provider.userId].headKey,
                  child: MahjongCommon.getUserHeader(provider),
                )),
            initReading(provider.prepare),
            provider.listening
                ? Positioned(
                    top: ScreenUtil().screenHeight / 3 -
                        MahjongSystemConst.actionTipsWidth,
                    right: MahjongSystemConst.edgeMargin,
                    child: SafeArea(
                      child: Image.asset(
                        "images/mahjong/action/CARD_CALL_TIPS.png",
                        height: MahjongSystemConst.actionTipsWidth,
                        width: MahjongSystemConst.actionTipsWidth,
                        fit: BoxFit.cover,
                      ),
                    ))
                : Container(),
            // 出牌的位置
            Positioned(
                top: MahjongSystemConst.lrbtMagin,
                left: MahjongSystemConst.lrlrMagin,
                child: RotationTransition(
                    turns: new AlwaysStoppedAnimation(3 / 4),
                    child:
                        MahjongCommon.getUserExportCard(provider, resv: true))),
            // 手上的牌的位置
            // 手牌位置  竖向的牌 明显比 碰牌竖向站位更大  所以这里需要加 4个像素
            Positioned(
                //MahjongSystemConst.bottomHandCardWidth * MahjongSystemConst.cardWHScale
                bottom: 0,
                left: MahjongSystemConst.lrUserCardMargin,
                child: Container(
                    alignment: Alignment.topRight,
                    width: MahjongSystemConst.lrUserCardWidth *
                        MahjongSystemConst.cardWHScale *
                        2,
                    height: MahjongSystemConst.lrCardWidth *
                        MahjongSystemConst.cardLrMultiple,
                    child: Stack(
                      children: buildUserCard(provider),
                    ))),
            // 交换牌的位置
            SwapMahjongWidget(
              swap: provider.swap,
              position: UserPosition.RIGHT,
            ),
            Positioned(
              left: MahjongSystemConst.lrlrMagin +
                  MahjongSystemConst.exportAreaSize -
                  MahjongSystemConst.actionTipsWidth,
              top: ScreenUtil().screenHeight / 2 -
                  MahjongSystemConst.actionTipsWidth / 2,
              child: AnimatedOpacity(
                opacity:
                    (provider.userDoAction != null && provider.showDoAction)
                        ? 1
                        : 0,
                duration: Duration(seconds: 1),
                child: provider.showDoAction
                    ? Image.asset(
                        "images/mahjong/action_tips/" +
                            provider.userDoAction +
                            ".png",
                        height: MahjongSystemConst.actionTipsWidth,
                        width: MahjongSystemConst.actionTipsWidth,
                        fit: BoxFit.cover,
                      )
                    : SizedBox(),
              ),
            ),
          ],
        );
      }
    });
  }

  RightUserInfo buildUserStateText(RightUserInfo provider) => provider;

  List<Widget> buildUserCard(RightUserInfo provider) {
    List<UserTouchBar> data = provider.userTouchBar;
    int number = provider.cardCount;
    List<Widget> result = [];
    var exporTag = (number % 3 == 2);
    double top = MahjongSystemConst.lrCardWidth;
    double margin = 0;
    if (provider.win) {
      result.add(Positioned(
          top: margin,
          left: 0,
          child: RotationTransition(
              turns: new AlwaysStoppedAnimation(270 / 360),
              child: Container(
                width: top * MahjongSystemConst.cardWHScale,
                child: LeftGameCommons.buildExportCard(
                    MahjongCardEntity(cardId: provider.huCard), top),
              ))));
    }
    /**
     * 如果 不是当前玩家出牌 直接从第二个位置显示 避免 视图切换过大
     */
    for (int i = 0; i < number; i++) {
      if (exporTag && i >= 1) {
        margin = top * (i + 1);
      } else if (exporTag) {
        margin = top * i;
      } else {
        if (provider.win) {
          margin = top * (i + 1.5);
        } else {
          margin = top * (i + 2);
        }
      }
      if (provider.win) {
        result.add(Positioned(
            top: margin,
            left: 0,
            child: RotationTransition(
                turns: new AlwaysStoppedAnimation(90 / 360),
                child: Container(
                  alignment: Alignment.bottomRight,
                  width: MahjongSystemConst.lrUserCardWidth *
                      MahjongSystemConst.cardWHScale,
                  child:
                      LeftGameCommons.buildExportCard(MahjongCardEntity(), top),
                ))));
      } else {
        result.add(Positioned(
            top: margin,
            left: 0,
            child: Container(
              width: top,
              child: Image.asset(
                "images/mahjong/user/right/usercard.png",
                fit: BoxFit.cover,
              ),
            )));
      }
    }
    if (provider.win) {
      margin = margin + top * 2;
    } else {
      margin = margin + top * 2.5;
    }
    top = MahjongSystemConst.lrUserCardWidth;
    for (int i = 0; i < data.length; i++) {
      if (i > 0) {
        margin = margin + top * 1.5;
      }
      int maxNumber = 3;
      for (int j = 0; j < maxNumber; j++) {
        int cardId = data[i].cards[j].cardId;
        double cardRight = 0;
        if (data[i].cards.length == 4 &&
            j != 1 &&
            data[i].cardEnum == CardEnum.ANGANG) {
          cardId = null;
        } else if (data[i].cards.length == 4 &&
            j == 1 &&
            data[i].cardEnum == CardEnum.ANGANG) {
          result.add(Positioned(
              top: margin + top * j,
              left: 0,
              child: RotationTransition(
                  turns: AlwaysStoppedAnimation(90 / 360),
                  child: Container(
                      width: MahjongSystemConst.lrUserCardWidth *
                          MahjongSystemConst.cardWHScale,
                      child: LeftGameCommons.buildExportCard(
                          MahjongCardEntity(cardId: null),
                          MahjongSystemConst.lrUserCardWidth)))));
          cardRight = MahjongSystemConst.lrUserCardWidth *
              MahjongSystemConst.cardWHScale *
              MahjongSystemConst.cardOtherScale;
        }
        result.add(Positioned(
            top: margin + top * j,
            left: cardRight,
            child: RotationTransition(
                turns: cardId == null
                    ? AlwaysStoppedAnimation(90 / 360)
                    : AlwaysStoppedAnimation(270 / 360),
                child: Container(
                  width: MahjongSystemConst.lrUserCardWidth *
                      MahjongSystemConst.cardWHScale,
                  child: LeftGameCommons.buildExportCard(
                      MahjongCardEntity(cardId: cardId), top),
                ))));
        if (data[i].cards.length == 4 &&
            j == 1 &&
            data[i].cardEnum != CardEnum.ANGANG) {
          result.add(Positioned(
              top: margin + top * j,
              left: MahjongSystemConst.lrUserCardWidth *
                  MahjongSystemConst.cardWHScale *
                  MahjongSystemConst.cardOtherScale,
              child: RotationTransition(
                  turns: AlwaysStoppedAnimation(270 / 360),
                  child: Container(
                      width: MahjongSystemConst.lrUserCardWidth *
                          MahjongSystemConst.cardWHScale,
                      child: LeftGameCommons.buildExportCard(
                          MahjongCardEntity(cardId: cardId),
                          MahjongSystemConst.lrUserCardWidth)))));
        }
        if (j == maxNumber - 1) {
          margin = margin + top * j;
        }
      }
    }
    return result;
  }

  initReading(bool prepare) {
    return prepare
        ? Positioned(
            top: ScreenUtil().screenHeight / 3,
            right: MahjongSystemConst.edgeMargin +
                MahjongSystemConst.userHeaderImageWidth,
            child: SafeArea(
              child: Container(
                height: MahjongSystemConst.userHeaderImageWidth *
                    MahjongSystemConst.cardMinWHScale,
                width: MahjongSystemConst.userHeaderImageWidth *
                    MahjongSystemConst.cardMinWHScale,
                child: Image.asset(
                  "images/mahjong/commons/reading.png",
                  fit: BoxFit.contain,
                ),
              ),
            ))
        : SizedBox();
  }
}
